package com.ruoyi.courseManagement.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.courseManagement.domain.Chapter;
import com.ruoyi.courseManagement.domain.vo.ChapterVO;
import com.ruoyi.courseManagement.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程章节Controller
 * 
 * @author liao
 * @date 2025-06-10
 */
@RestController
@RequestMapping("/courseManagement/chapter")
public class ChapterController extends BaseController
{
    @Autowired
    private ChapterService chapterService;

    /**
     * 查询课程章节列表
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:chapter:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChapterVO chapterVO)
    {
        System.out.println(chapterVO+"--------------------------");
        startPage();
        List<ChapterVO> list = chapterService.selectChapterList(chapterVO);

        return getDataTable(list);
    }

    /**
     * 导出课程章节列表
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:chapter:export')")
    @Log(title = "课程章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ChapterVO chapterVO)
    {
        List<ChapterVO> list = chapterService.selectChapterList(chapterVO);
        ExcelUtil<ChapterVO> util = new ExcelUtil<ChapterVO>(ChapterVO.class);
        util.exportExcel(response, list, "课程章节数据");
    }

    /**
     * 获取课程章节详细信息
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:chapter:query')")
    @GetMapping(value = "/{chapterId}")
    public AjaxResult getInfo(@PathVariable("chapterId") Integer chapterId)
    {
        return success(chapterService.selectChapterByChapterId(chapterId));
    }

    /**
     * 新增课程章节
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:chapter:add')")
    @Log(title = "课程章节", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Chapter chapter)
    {
        return toAjax(chapterService.insertChapter(chapter));
    }

    /**
     * 修改课程章节
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:chapter:edit')")
    @Log(title = "课程章节", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Chapter chapter)
    {
        return toAjax(chapterService.updateChapter(chapter));
    }

    /**
     * 删除课程章节
     */
    @PreAuthorize("@ss.hasPermi('courseManagement:chapter:remove')")
    @Log(title = "课程章节", businessType = BusinessType.DELETE)
	@DeleteMapping("/{chapterIds}")
    public AjaxResult remove(@PathVariable Integer[] chapterIds)
    {
        return toAjax(chapterService.deleteChapterByChapterIds(chapterIds));
    }
}
